package io.renren.modules.project.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.project.entity.ProPlanEntity;
import io.renren.modules.project.service.ProPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static io.renren.common.utils.ShiroUtils.getUserId;

@RestController
@RequestMapping(value = "/project/plan")
public class ProPlanController {

    @Autowired
    private ProPlanService proPlanService;

    @GetMapping("/info/{planId}")
    public R info(@PathVariable("planId") Long planId) {
        ProPlanEntity proPlan = proPlanService.getById(planId);
        return R.ok().put("proPlan", proPlan);
    }

    @GetMapping(value = "list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = proPlanService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping(value = "list-all")
    public R listAll() {
        List<ProPlanEntity> page = proPlanService.list();

        return R.ok().put("page", page);
    }

    @PostMapping(value= "save")
    public R save(@RequestBody ProPlanEntity proPlan) {
        ValidatorUtils.validateEntity(proPlan);

        proPlan.setCreateTime(new Date());
        proPlan.setUpdateTime(null);
        proPlan.setCreateUserId(getUserId()); // 获取当前登录人
        proPlanService.save(proPlan);

        return R.ok();
    }

    @PostMapping(value= "update")
    public R update(@RequestBody ProPlanEntity proPlan) {
        ValidatorUtils.validateEntity(proPlan);

        proPlan.setUpdateTime(new Date());
        proPlan.setUpdateUserId(getUserId()); // 获取当前登录人

        proPlanService.saveOrUpdate(proPlan);

        return R.ok();
    }

    @PostMapping(value = "delete")
    public R delete(@RequestBody int[] planIds) {
        proPlanService.deleteBatch(planIds);

        return R.ok();
    }
}
